/*
 * @Author: szx
 * @Date: 2022-04-01 12:51:55
 * @LastEditTime: 2022-04-01 13:37:45
 * @Description:
 * @FilePath: \leetcode\900-999\954\954.js
 */
/**
 * @param {number[]} arr
 * @return {boolean}
 */
var canReorderDoubled = function (arr) {
    const n = arr.length;
    const map = new Map();
    for (let i of arr) map.set(i, (map.get(i) || 0) + 1);
    const vals = Array.from(map.keys()).sort((a, b) => Math.abs(a) - Math.abs(b));
    for (let i of vals) {
        const num = 2 * i;
        if ((map.get(num) || 0) < map.get(i)) return false;
        map.set(num, map.get(num) - map.get(i));
    }
    return true;
};
console.log(canReorderDoubled([2, 1, 2, 1, 1, 1, 2, 2]));
console.log(canReorderDoubled([1, 2, 4, 16, 8, 4]));
console.log(canReorderDoubled([1, 1, 2, 8]));
console.log(canReorderDoubled([2, 4, 0, 0, 8, 1]));
console.log(canReorderDoubled([3, 1, 3, 6]));
console.log(canReorderDoubled([2, 1, 2, 6]));
console.log(canReorderDoubled([4, -2, 2, -4]));
